package com.linsir.utils;

import com.linsir.lin.dto.SysMenuDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName:linsir-lin
 * @Package:com.linsir.utils
 * @Title: TreeUtil
 * @Description: TODO
 * @Create:2021/6/6 21:40 by @author:Linsir
 * @Update:2021/6/6 21:40 by @author:Linsir
 * @UpdateRemark: 初始创建
 * @Version 1.0.0
 */
public class TreeUtil {

    /**
     *
     * 菜单树形结构
     * @param treeList
     * @param pid
     * @return
     */

    public static List<SysMenuDTO> toTree(List<SysMenuDTO> treeList,Integer pid)
    {

        List<SysMenuDTO> retList = new ArrayList<SysMenuDTO>();

        for (SysMenuDTO parent : treeList) {
            if (pid.equals(parent.getPid())) {
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }


    /**
     * 根据菜单属性结构
     * @param parent
     * @param treeList
     * @return
     */
    private static SysMenuDTO findChildren(SysMenuDTO parent, List<SysMenuDTO> treeList)
    {
            for (SysMenuDTO child : treeList)
            {
                if (parent.getId().equals(child.getPid()))
                {
                    if (parent.getChild()==null)
                    {
                        parent.setChild(new ArrayList<>());
                    }
                    parent.getChild().add(findChildren(child,treeList));
                }
            }
            return parent;
    }

}
